#include "bsp_sl2823.h"

#include <string.h>
#include <stdint.h>

#include "emv_if.h"
#include "Rfid_interface.h"
#include "iso14443_4.h"

#include "lpcd.h"
#include "sl2x23.h"

// extern uint8_t irq_flag_io;

int sl2823_init(void)
{
    delay_1ms(2);
    spi_wr_test();
    Rfid_Init();
    pcd_default_info();
    // irq_flag_io = 0;

#ifdef LPCD_MODE

    //lpcd_cfg 在 lpcd.h 有extern
    lpcd_cfg.t1 = LPCD_DETECT_PERIOD;      //周期  0x12 300ms; //0x20 500ms ; //0x38 800ms
    lpcd_cfg.sense = LPCD_DETECT_SENS;      //电流
    osc_calib();    //时钟初始化
    lpcd_init(1, 0x40); //fst_en    第一次lpcd初始化
    lpcd_entry();   //进入lpcd模式

#endif

    return 0;
}

typedef enum
{
    NFC_DETECT_STATE_SEEK,
    NFC_DETECT_STATE_RECOGNIZE,
    NFC_DETECT_STATE_CLEAR,
}nfc_detcet_state_t;

typedef enum
{
    NFC_DECTECT_RET_NOCARD,
    NFC_DECTECT_RET_UNKNOWN_CARD,
    NFC_DECTECT_RET_VALID_CARD
}nfc_dectect_ret_t;

extern uint8_t card_A;
extern uint8_t card_B;
extern uint8_t card_F;
extern uint8_t card_V;
extern uint8_t g_ask_block_flag;
extern uint8_t  sak;
extern uint16_t atqa;

static int nfc_detect(const uint8_t *expected_uid, uint8_t uid_len)
{
    volatile uint8_t detect_state = NFC_DETECT_STATE_SEEK;
    uint8_t is_valid_card = 0;  // 0: 无效， 1：有效但未验证 2：已验证
    while(1) {
        switch(detect_state) {
        case NFC_DETECT_STATE_SEEK://询卡
            //类型标志置0
            card_A = card_B = 0;
            // card_F = 0;
            // card_V = 0;
            g_ask_block_flag = 0;

            //设置通信速率并轮询卡啊片
            pcd_set_rate(1);    // 106kbps
            if(pcd_polling(g_tag_info.serial_num) == 0) //打开场强，检测卡片
            {
                detect_state = NFC_DETECT_STATE_RECOGNIZE;  //检测到卡
            }
            else
            {
                return 0;   //无卡片
            }
            break;
        case NFC_DETECT_STATE_RECOGNIZE:
            if(card_A) {
                // 验证UID是否匹配
                if(memcmp(g_tag_info.serial_num, expected_uid, uid_len) == 0)
                {
                    printf("[nfc_detect] UID匹配!\r\n");
                    is_valid_card = 2;  //2：已验证
                }
                else
                {
                    printf("[nfc_detect] uid_length:%d 未知卡片UID: \r\n", g_tag_info.uid_length);

                    printf("%02x %02x %02x %02x\r\n", (g_tag_info.serial_num)[0], (g_tag_info.serial_num)[1], (g_tag_info.serial_num)[2], g_tag_info.serial_num[3]);


                    PRINT_BUF("", g_tag_info.serial_num, g_tag_info.uid_length);
                    is_valid_card = 1;  //1：有效但未验证 
                }

                printf("sak == %02x\r\n\r\n",sak);
                
            }
           
            detect_state = NFC_DETECT_STATE_CLEAR;
            break;
        case NFC_DETECT_STATE_CLEAR:
            pcd_antenna_off();  //关闭场强
            // delay_1ms(1);
            
            return is_valid_card;
            
        default:
            detect_state = NFC_DETECT_STATE_SEEK;
            is_valid_card  = 0;
            break;
        }
    }
}

const uint8_t valid_uid[] = {0x9a, 0x85, 0xe4, 0x00};

int ls2823_loop(void)
{
    uint8_t is_valid_card = 0;

    is_valid_card = nfc_detect(valid_uid, sizeof(valid_uid));

    LOS_Msleep(200);

    return is_valid_card;
}

int sl2823_lpcd_loop(void) {
    // static uint32_t wait_cnt = 0;

    uint8_t adc_value = 0;
    
    uint8_t is_valid_card = 0;
    // irq_flag_io = 0;
    
    // printf("******* enter sl2823_lpcd_loop() *******\r\n");

    if(!(INT_PIN == 0))     //读寄存器的方式获取中断
    {
        // printf("...................................\r\n");
        // printf("\r\n******* 1. pcd_exit() *********\r\n");
        adc_value = lpcd_exit();
        Rfid_Init();

        // printf("******* 2. nfc_detect() *********\r\n");
        is_valid_card = nfc_detect(valid_uid, sizeof(valid_uid));
        lpcd_init((is_valid_card != 0 ? 1 : 0), adc_value);    //自动调节adc_value
        // pcd_antenna_off();

        // printf("******* 3. lpcd_entry() *******\r\n");
        lpcd_entry();
        // delay_1ms(100);
        // irq_flag_io = 0;
        
    }
    // else {
    //     wait_cnt++;
    //     if(wait_cnt == 500000) {
    //         wait_cnt = 0;
    //         // printf(".");
    //     }
    // }
        
    return is_valid_card;
}
